$(function () {
    $(".menu-toggle").on('click',function () {
        if($(this).attr('class')=='menu-toggle'){
            $(this).addClass('active');
            $('#background').fadeIn();
            $('.header-box ul').show().animate({'right':"0"});
        }else {
            $(this).removeClass('active');
            $('#background').fadeOut();
            $('.header-box ul').animate({'right':"-100%"},function () {
                $(this).hide();
            });
        }
    });
    $('#background').on('click',function () {
        $('.menu-toggle').removeClass('active');
        $('#background').fadeOut();
        $('.header-box ul').animate({'right':"-100%"},function () {
            $(this).hide();
        });
    })

    //注册
    var reg = $("#registerBox");
    reg.find('input[type="button"]').on('click',function () {
        var nickname = reg.find('[name="nickname"]').val(),
            password = reg.find('[name="password"]').val(),
            repassword = reg.find('[name="repassword"]').val();
        $.ajax({
            type:'post',
            url:'/reg',
            data: {
                nickname:nickname,
                password:password,
                repassword:repassword
            },
            dataType:'json',
            success: function (result) {
                reg.find('.other-text').html(result.message);
                if(!result.code){
                    alert(result.message+',请记住您的YD账号为：'+result.userInfo.YD);
                    window.document.location.href="/";
                }
            }
        })

    })

    //登录
    var log = $("#loginBox");
    log.find('input[type="button"]').on("click",function(){
        var userYD = log.find('[name="userYD"]').val(),
            password = log.find('[name="password"]').val();
        $.ajax({
            type:'post',
            url:'/log',
            data:{
                YD:userYD,
                password:password
            },
            dataType:'json',
            success :function(result) {
                log.find('.other-text').html(result.message);
                if(!result.code){
                    setTimeout(function(){
                        window.document.location.href="/";
                    },1000)
                }
            }
        })
    })

    //退出
    $('.logout').on('click', function() {
        $.ajax({
            url: '/log/logout',
            success: function(result) {
                if (!result.code) {
                    window.location.reload();
                }
            }
        });
    })

})